import { PointInterface } from "./point-related/point-interface";
import { PointStyle } from "./point-related/point-style";
import { PointBase } from "./point-related/point-base";
import { ShapeBoxBase } from "../component-model/component-related/box-base";
import { CommonUtil } from "../../../util/common-util";

export class ControllerPoint extends PointBase implements PointInterface {
    style: PointStyle;
    constructor(data) {
        super(data);
    }
    start(e) {
        for (let point of this.belongTo.points[0]) {
            point.oldStyle = JSON.parse(JSON.stringify(point.style));
            point.oldPlace = {
                x: e.clientX,
                y: e.clientY
            }
        }
    }
    move(e) {
        super.move(e);
        let op = {
            "nw-resize": "se-resize",
            "ne-resize": "sw-resize",
            "sw-resize": "ne-resize",
            "se-resize": "nw-resize"
        }
        let wharr = [
            [-1, -1, 1, -1],
            [1, 1, 1, -1],
            [1, 1, -1, 1],
            [-1, -1, -1, 1]
        ];
        let fixedPoint: ControllerPoint;
        let wh;
        for (let i = 0; i < this.belongTo.points[0].length; i++) {
            let point: PointBase = this.belongTo.points[0][i];
            if (point.style.cursor == op[this.style.cursor]) {
                fixedPoint = point;
            }
            if (point == this) {
                wh = wharr[i];
            }
        }
        //计算canvas的长和宽方向获取的分量
        let movement = this.getMovement(e);
        let nwidth = this.belongTo.oldStyle.rwidth + wh[0] * movement.x * this.belongTo.baseData.cos + wh[1] * movement.y * this.belongTo.baseData.sin;
        let nheight = this.belongTo.oldStyle.rheight + wh[2] * movement.x * this.belongTo.baseData.sin + wh[3] * movement.y * this.belongTo.baseData.cos;
        nwidth = Math.max(nwidth, 20);
        nheight = Math.max(nheight, 20);
        this.belongTo.scale({
            x: nwidth / this.belongTo.oldStyle.rwidth,
            y: nheight / this.belongTo.oldStyle.rheight,
            fixedX: CommonUtil.px2num(fixedPoint.oldStyle.left) + fixedPoint.pwh / 2,
            fixedY: CommonUtil.px2num(fixedPoint.oldStyle.top) + fixedPoint.pwh / 2
        });
    }
}